package com.zzuli.yxy.task.springDataPipeline;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzuli.yxy.entity.Song;
import com.zzuli.yxy.util.TXCloudFileUtils;
import com.zzuli.yxy.mapper.SongMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 会获取一种数据
 * song 的二进制数据
 * 将 song 写入 .mp3 文件，并将 .mp3 上传到云端的存储桶里
 * 然后将云端 .mp3文件 路径保存到数据库
 * @auther pony
 * @create 2023-04-05 19:06
 */
@Component
public class SongLoadPipeline implements Pipeline {

    @Autowired
    private SongMapper songMapper;

    //云端的存储桶路径
    private static String imgPath = "yxy/songs/";

    //腾讯云对象存储 路径-访问域名
    private static String url = "https://pony-1312090731.cos.ap-beijing.myqcloud.com";

    @Override
    public void process(ResultItems resultItems, Task task) {
        // 将 songBytes 二进制数据 ，写入文件
        // 1、获取歌曲id
        String songId = resultItems.get("songId");
        // 2、查询数据库
        LambdaQueryWrapper<Song> qw = new LambdaQueryWrapper<>();
        qw.eq(Song::getSongId, songId);
        Song song = songMapper.selectOne(qw);
        System.out.println(song);
        // File类打开本地文件 F:\网易云音乐-热门榜
        // 本地桌面地址
//        File directory = FileSystemView.getFileSystemView().getHomeDirectory();
//        System.out.println("directory:"+directory);
        File file = new File("F:\\网易云音乐-热门榜", song.getSongName() + ".mp3");
        // 3、获取 songBytes，二进制数据
        byte[] songBytes = resultItems.get("songBytes");
        System.out.println("songBytes:"+ songBytes);
        // 4、判断文件或者文件夹是否存在，不存在就创建
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 5、准备一个文件输出流，将二进制流写入文件
        byte[] bytes = new byte[1024*1024];

        try(
           // 使用缓冲流，内置一个8k的字节数组，防止文件过大，内存消耗较大，导致的写入卡顿
           BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        ) {
            outputStream.write(songBytes);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 将文件上传到
        String songKey = TXCloudFileUtils.upLoad(imgPath, TXCloudFileUtils.fileToMultipartFile(file));
        //7、拼接 song 访问路径，并写入数据库
        String songUrl = url + "/" + imgPath + songKey;
        System.out.println("imgUrl:"+songUrl);
        song.setSongUrl(songUrl);
        LambdaUpdateWrapper<Song> uw = new LambdaUpdateWrapper<>();
        uw.eq(Song::getSongId, songId);
        uw.set(Song::getSongUrl, songUrl);
        songMapper.update(song,uw);
    }
}
